package com.ning.weixin.service.impl;

import com.ning.weixin.entity.UserAddress;
import com.ning.weixin.mapper.UserAddressMapper;
import com.ning.weixin.service.UserAddressService;
import com.ning.weixin.utils.KeyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class UserAddressServiceImpl implements UserAddressService {

    @Autowired
    private UserAddressMapper mapper;

    @Override
    public void saveUserAddress(UserAddress userAddress,String openId) {
        if (openId != null){
            List<String> addressIdList = new ArrayList<>();
            addressIdList.add("1");
            addressIdList.add("2");
            addressIdList.add("3");
            addressIdList.add("4");
            addressIdList.add("5");
            addressIdList.add("6");
            addressIdList.add("7");
            for (String addressId:addressIdList){
                userAddress.setId(KeyUtil.genUniqueKey());
                userAddress.setOpenId(openId);
                userAddress.setAddressId(addressId);
                mapper.saveUserAddress(userAddress);
            }
        }
    }

    @Override
    public List<String> findByOpenId(String openId) {
        if (openId != null){
            return mapper.findByOpenId(openId);
        }
        return null;
    }

    @Override
    public void subDownClockNum(UserAddress userAddress) {
        if (userAddress != null){
            mapper.subDownClockNum(userAddress);
        }
    }

    @Override
    public List<Map<String, Object>> findAddressById(String openId) {
        if (openId != null){
            return mapper.findAddressById(openId);
        }
        return null;
    }
}
